/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.com.minam.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pe.com.minam.beans.Lugar;
import pe.com.minam.beans.RespuestaAjax;
import pe.com.minam.service.DepaService;
import pe.com.minam.service.InfoAireService;
import pe.com.minam.service.LugarCalidadService;
import pe.com.minam.util.ParameterProperties;
import pe.com.minam.util.PropertiesInterno;
import pe.com.minam.util.Util;

/**
 *
 * @author Willian
 */
@Controller
@RequestMapping("/*")
public class MainController {
    
    private static final Logger logger = Logger.getLogger(MainController.class.getName());
    
    @Autowired
    private ParameterProperties parameterProperties;
    
    @Autowired
    private LugarCalidadService lugarCalidadService;
    
    @Autowired
    private InfoAireService infoAireService;
    
    @Autowired
    private DepaService depaService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String indexView(Model model) {
        return parameterProperties.JSP_INICIO;
    }
    
    @RequestMapping(value = "/objetivo", method = RequestMethod.GET)
    public String objetivoView(Model model) {
        return parameterProperties.JSP_OBJETIVO;
    }
    
    @RequestMapping(value = "/MapaDelPortal", method = RequestMethod.GET)
    public String mapadelsitioView(Model model) {
        return parameterProperties.JSP_MAPA_SITIO;
    }
    
    @RequestMapping(value = "/FAQ", method = RequestMethod.GET)
    public String faqView(Model model) {
        return parameterProperties.JSP_FAQ;
    }
    
    @RequestMapping(value = "/Contacto", method = RequestMethod.GET)
    public String contactoView(Model model) {
        return parameterProperties.JSP_CONTACTO;
    }
    
    @RequestMapping(value = "/formContacto", method = RequestMethod.POST,produces=MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public RespuestaAjax formContactoAjax(@RequestParam("nombreContacto") String nombre,
            @RequestParam("asuntoContacto") String asunto,
            @RequestParam("emailContacto") String email,
            @RequestParam("mensajeContacto") String mensaje){
        RespuestaAjax rpta = new RespuestaAjax();
        rpta.setStatus(PropertiesInterno.CODIGO_ERROR);
        try{
            mensaje = "DE: ["+nombre+"] "+email + " "+mensaje;
            String destino = PropertiesInterno.CORREO_RECEPCION;
            if(Util.EnviarMailNormal(destino,asunto,mensaje)){
                rpta.setStatus(PropertiesInterno.CODIGO_EXITO);
            }
        }catch(Exception ex){
            logger.log(Level.WARNING, "Error al enviar mensaje email: {0}",new Object[]{ex});
        }
        return rpta;
    }
    
    @RequestMapping(value = "/concepto", method = RequestMethod.GET)
    public String conceptoView(Model model) {
        return parameterProperties.JSP_CONCEPTO;
    }
    
    @RequestMapping(value = "/fuentes", method = RequestMethod.GET)
    public String fuentesView(Model model) {
        return parameterProperties.JSP_FUENTES_CONTAMINANTES;
    }
    
    /**
     ************************************************************************************* 
     *************************************************************************************
     */
    @RequestMapping(value = "/inventario_de_estaciones", method = RequestMethod.GET)
    public String getInventario_de_estaciones(Model model) {
        return parameterProperties.JSP_INVENTARIO_ESTACIONES;
    }
    
    @RequestMapping(value = "/calidad_del_aire", method = RequestMethod.GET)
    public String getCalidadDelAire(Model model) {
        return parameterProperties.JSP_CALIDAD_DEL_AIRE;
    }
    
    @RequestMapping(value = "/lista_noticias", method = RequestMethod.GET)
    public String getNoticias(Model model) {
        return parameterProperties.JSP_NOTICIAS;
    }
    
    @RequestMapping(value = "/normativa_ambiental", method = RequestMethod.GET)
    public String getNormativa(Model model) {
        return parameterProperties.JSP_NORMATIVA;
    }
    
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String getLogin(Model model) {
        return parameterProperties.JSP_LOGIN;
    }
    
    @RequestMapping(value = "/denied", method = RequestMethod.GET)
    public String getDenied(Model model) {
        return parameterProperties.JSP_DENIED;
    }
    
    @RequestMapping(value = "/error", method = RequestMethod.GET)
    public String getError(Model model) {
        return parameterProperties.JSP_ERROR;
    }
    
    @RequestMapping(value = "/mapa", method = RequestMethod.GET)
    public String getMapa(Model model) {
        model.addAttribute("ltaDepas", depaService.getDepas());
        return parameterProperties.JSP_MAPA;
    }
    
    @RequestMapping(value = "/getAllLugar", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public List<Lugar> getAllLugares(@RequestParam("departamento") String depa,
            @RequestParam("bueno") boolean bueno,
            @RequestParam("regular") boolean regular,
            @RequestParam("malo") boolean malo,
            @RequestParam("pesimo") boolean pesimo) {
        List<Lugar> lta = new ArrayList<Lugar>();
        try{
            logger.info("Obteniendo lugares con la calidad establecida");
            if(bueno){
                lta.addAll(lugarCalidadService.getLugaresPorDepartamento(depa, "B"));
                logger.info("Obteniendo lugares con la calidad buena");
            }
            if(regular){
                lta.addAll(lugarCalidadService.getLugaresPorDepartamento(depa, "R"));
                logger.info("Obteniendo lugares con la calidad regular");
            }
            if(malo){
                lta.addAll(lugarCalidadService.getLugaresPorDepartamento(depa, "M"));
                logger.info("Obteniendo lugares con la calidad mala");
            }
            if(pesimo){
                lta.addAll(lugarCalidadService.getLugaresPorDepartamento(depa, "P"));
                logger.info("Obteniendo lugares con la calidad pesima");
            }            
            return lta;
        }catch(Exception ex){
            logger.log(Level.INFO, "Exception: ",ex);
            return null;
        }
    }
    
    @RequestMapping(value = "/getdatosaire", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public String getDatosAire(@RequestParam("tipo") String tipo, @RequestParam("lugar") String lugar) {
        try{
            String rpta = infoAireService.getDataForGraphic(tipo, lugar);
            return rpta != null ? rpta.replaceAll("'", "\"") : rpta;
        }catch(Exception ex){
            logger.log(Level.INFO, "Exception: ",ex);
            return null;
        }
    }
    
    @RequestMapping(value = "/getdatosairetable", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public String getDatosAireTable(@RequestParam("lugar") String lugar) {
        try{
            String rpta = infoAireService.getDataForTable(lugar);
            return rpta != null ? rpta : "error";
        }catch(Exception ex){
            logger.log(Level.INFO, "Exception: ",ex);
            return "error";
        }
    }
    
    @RequestMapping(value = "/getdatosairetablehorarios", method = RequestMethod.GET, produces = "text/html")
    @ResponseBody
    public String getDatosAireTableHorarios(@RequestParam("lugar") String lugar) {
        try{
            String rpta = infoAireService.getDataForTableHorarios(lugar);
            return rpta != null ? rpta : "error";
        }catch(Exception ex){
            logger.log(Level.INFO, "Exception: ",ex);
            return "error";
        }
    }
}
